#!/bin/bash
# Python3 和 pip3 安装脚本
# 自动检测Linux发行版并安装Python3和pip3

echo "=========================================="
echo "Python3 和 pip3 安装脚本"
echo "=========================================="
echo ""

# 检测Linux发行版
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS=$ID
    VERSION=$VERSION_ID
else
    echo "❌ 无法检测操作系统版本"
    exit 1
fi

echo "检测到系统: $OS $VERSION"
echo ""

# 检查是否有root权限
if [ "$EUID" -ne 0 ]; then 
    echo "⚠️  需要root权限，请使用 sudo 运行此脚本"
    echo "sudo ./install_python.sh"
    exit 1
fi

# 根据不同的发行版安装
case $OS in
    ubuntu|debian)
        echo "📦 Ubuntu/Debian 系统，使用 apt 安装..."
        echo ""
        
        echo "[1/4] 更新包列表..."
        apt update
        
        echo ""
        echo "[2/4] 安装 Python3..."
        apt install -y python3
        
        echo ""
        echo "[3/4] 安装 pip3..."
        apt install -y python3-pip
        
        echo ""
        echo "[4/4] 安装 python3-venv（虚拟环境）..."
        apt install -y python3-venv
        ;;
        
    centos|rhel|fedora)
        echo "📦 CentOS/RHEL/Fedora 系统，使用 yum/dnf 安装..."
        echo ""
        
        if command -v dnf &> /dev/null; then
            PKG_MGR="dnf"
        else
            PKG_MGR="yum"
        fi
        
        echo "[1/3] 安装 Python3..."
        $PKG_MGR install -y python3
        
        echo ""
        echo "[2/3] 安装 pip3..."
        $PKG_MGR install -y python3-pip
        
        echo ""
        echo "[3/3] 安装开发工具..."
        $PKG_MGR install -y python3-devel
        ;;
        
    *)
        echo "❌ 不支持的操作系统: $OS"
        echo "请手动安装 Python3 和 pip3"
        exit 1
        ;;
esac

echo ""
echo "=========================================="
echo "验证安装..."
echo "=========================================="

# 验证Python3
if command -v python3 &> /dev/null; then
    PYTHON_VERSION=$(python3 --version)
    echo "✅ Python3: $PYTHON_VERSION"
else
    echo "❌ Python3 安装失败"
    exit 1
fi

# 验证pip3
if command -v pip3 &> /dev/null; then
    PIP_VERSION=$(pip3 --version)
    echo "✅ pip3: $PIP_VERSION"
else
    echo "❌ pip3 安装失败"
    exit 1
fi

echo ""
echo "=========================================="
echo "🎉 安装完成！"
echo "=========================================="
echo ""
echo "接下来运行："
echo "  ./deploy.sh"
echo ""

